package com.restkeeper.dubbo;


import com.restkeeper.tenant.TenantContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * packageName com.restkeeper.dubbo
 *
 * @author 邵一宸
 * @version JDK 17
 * @className DubboConsumerContextFilter
 * @date 2024/7/28
 * @description TODO
 */
@Activate
@Slf4j
public class DubboConsumerContextFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

//        log.info("shopId------------"+ RpcContext.getContext().getAttachment("shopId"));
//
//        log.info("ThreadName---------"+Thread.currentThread().getName());

        // 从自定义的上下文对象钟获取到token的相关信息，然后存入到RpcContext
        RpcContext.getContext().setAttachment("shopId", TenantContext.getShopId());
        RpcContext.getContext().setAttachment("loginUserId", TenantContext.getLoginUserId());
        RpcContext.getContext().setAttachment("loginUserName", TenantContext.getLoginUserName());
        RpcContext.getContext().setAttachment("storeId", TenantContext.getStoreId());
        return invoker.invoke(invocation);
    }
}